﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Drawing;
using System.Xml;

namespace loginSQL.Util
{
    public class BookData
    {
        List<PageData> pagedata;

        public String Name
        {
            get;
            set;
        }

        public List<PageData> PageData
        {
            get
            {
                return pagedata;
            }
        }

        public BookData()
        {
            pagedata = new List<PageData>();
        }

        public void Save(String filename)
        {
            XmlWriterSettings settings = new XmlWriterSettings();
            settings.Indent = true;
            settings.IndentChars = "\t";
            using (XmlWriter xw = XmlWriter.Create(filename,settings))
            {
                xw.WriteStartDocument();
                xw.WriteStartElement("book");
                xw.WriteAttributeString("name", Name);
                foreach (PageData page in pagedata)
                {
                    xw.WriteStartElement("page");
                    xw.WriteAttributeString("background", page.BackgroundFile);
                    foreach (KeyValuePair<String, PointF> pair in page.DataSource)
                    {
                        xw.WriteStartElement("data");
                        xw.WriteAttributeString("name", pair.Key);
                        xw.WriteAttributeString("posx", pair.Value.X.ToString());
                        xw.WriteAttributeString("posy", pair.Value.Y.ToString());
                        xw.WriteAttributeString("visibility", page.IsVisible(pair.Key).ToString());
                        xw.WriteEndElement();
                    }
                    xw.WriteEndElement();
                }
                xw.WriteEndElement();
            }
        }

        public static BookData Load(String filename)
        {
            BookData bookData = null;
            using (XmlReader reader = XmlReader.Create(filename))
            {
                reader.ReadToFollowing("book");
                bookData = new BookData();
                PageData pageData = null;
                bookData.Name = reader.GetAttribute("name");
                while (reader.Read())
                {
                    switch (reader.NodeType)
                    {
                        case XmlNodeType.Element:
                            if (reader.Name.Equals("page", StringComparison.OrdinalIgnoreCase))
                            {
                                pageData = new PageData();
                                String bgFile = reader.GetAttribute("background");
                                if (bgFile == null)
                                {
                                    bgFile = "";
                                }
                                pageData.BackgroundFile = bgFile;
                            }
                            else if (reader.Name.Equals("data", StringComparison.OrdinalIgnoreCase))
                            {
                                String name = reader.GetAttribute("name");
                                float x = float.Parse(reader.GetAttribute("posx"));
                                float y = float.Parse(reader.GetAttribute("posy"));
                                bool visibility=bool.Parse(reader.GetAttribute("visibility"));
                                PointF pointf = new PointF(x, y);
                                pageData.SetData(name, pointf);
                                pageData.SetVisibility(name, visibility);
                            }
                            break;
                        case XmlNodeType.EndElement:
                            if (reader.Name.Equals("page", StringComparison.OrdinalIgnoreCase))
                            {
                                bookData.PageData.Add(pageData);
                            }
                            break;
                    }
                }
            }
            return bookData;
        }

        public override string ToString()
        {
            return "{Name : " + Name+"}";
        }

        public int PageCount 
        {
            get
            {
                return pagedata.Count;
            }
        }

        public void AddNewPage()
        {
            PageData page = new PageData();
            PageData.Add(page);
        }
    }
}
